package com.ParkingManager.app.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.ParkingManager.app.bean.User;
import com.ParkingManager.app.dao.CarIOMapper;
import com.ParkingManager.app.service.DoorKeeperService;
import com.ParkingManager.app.service.ImgService;
import com.ParkingManager.app.service.RecordService;
import com.ParkingManager.app.util.PathManager;

@Service
public class DoorKeeperServiceImpl implements DoorKeeperService {

	@Autowired
	ImgService imgService;
	
	@Autowired
	CarIOMapper carIOMapper;
	
	@Autowired
	private RecordService recordService;
	
	@Autowired
	private HttpSession session;
	

	public String[] carImgDeal(MultipartFile file) {
		// TODO Auto-generated method stub
		String newFileName = imgService.saveImg(file);
		String[] carId = imgService.doLPR(PathManager.imagePath + File.separator + newFileName);
		String[] ret = new String[3];
		ret[0] = newFileName;
		if (carId == null) {
			ret[1] = "-1";
			ret[2] = "-1";
		}else{
			ret[1] = carId[0];
			ret[2] = carId[1];
		}
		return ret;
	}
	
	/**
	 * return recordId
	 */

	public int carIn(String carId,String carType,String image){
		User user = (User)session.getAttribute("user");
		int parkingId = carIOMapper.getParkingId(user.getUserName());
		System.out.println(parkingId);
		boolean ret = carIOMapper.saveCar(carId, carType=="大型车"?0:1, image, parkingId);
		return 1;
	}
	

	public boolean carOut(String carId,String carType,String image) {
		// TODO Auto-generated method stub
		return carIOMapper.carOut(carIOMapper.getRecordId(carId));
	}


	public HashMap<String, Object> findCar(String carId) {
		// TODO Auto-generated method stub
		
		return carIOMapper.findCar(carId);
	}
	
	public List<HashMap<String, Object>> listCar(){
		User user = (User)session.getAttribute("user");
		return carIOMapper.getAllCar(carIOMapper.getParkingId(user.getUserName()));
	}


	public int[] getCarMaxAndNow() {
		// TODO Auto-generated method stub
		int[] ret = new int[2];
		User user = (User)session.getAttribute("user");
		int parkingId = carIOMapper.getParkingId(user.getUserName());
		ret[0] = carIOMapper.getParkingMaxNum(parkingId);
		ret[1] = carIOMapper.getParkingNowNum(parkingId);
		return ret;
	}


	public int getRecordId(String carId) {
		// TODO Auto-generated method stub
		return carIOMapper.getRecordId(carId);
	}

	
}
